﻿#include "FITKAbaqusAdaptorMaterialDensity.h"
#include "InpKeyLineSpliter.h"

#include "FITKAbaqusINPReader.h"
#include "FITKAbaqusINPWriter.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterial.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialDensity.h"

namespace IO
{
    QString FITKAbaqusAdaptorMaterialDensity::getAdaptorClass()
    {
        return "IO::FITKAbaqusAdaptorMaterialDensity";
    }

    bool FITKAbaqusAdaptorMaterialDensity::adaptR()
    {
        
        _materialData = dynamic_cast<Interface::FITKAbaMaterial*>(_dataObj);
        if (!_materialData || !_reader) return false;

        //回退一行 处理关键字信息 校验
        _reader->backLine();

        Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialBehaviorType materialType = 
            Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialBehaviorType::Density;
        QString line = _reader->readLine();
        if (!line.toLower().startsWith("*density")) return false;
        bool create = _materialData->addMaterialBehavior(materialType);
        if (!create) return false;
        auto tempDensity = _materialData->getMaterialBehavior(materialType);
        auto densityData = dynamic_cast<Interface::FITKAbaMaterialDensity*>(tempDensity);
        if (!densityData) return false;

        QVector<double> MassDensitys;
        QVector<double> Temperatures;
        bool isRead = false;
        //解析 代码段
        while (!_reader->atEnd())
        {
            QString line = _reader->readLine();
            if (line.trimmed().isEmpty())continue;
            //关键字 段落结束
            if (line.startsWith("*"))
            {
                isRead = true;
                break;
            }
            QStringList sList = line.split(",");

            if (sList.size() < 2)
            {
                //可能的异常判断
                return false;
            }
            MassDensitys.append(sList[0].toDouble());
            Temperatures.append(sList[1].toDouble());
        }

        //随温度变化的状态
        if (MassDensitys.size() > 1) densityData->setUseTemperatureDependentData(true);
        //读取正常结束 讲数据插入到结构中
        if (isRead || _reader->atEnd())
        {
            densityData->setNumberOfRows(MassDensitys.size());
            for (int i = 0; i < MassDensitys.count(); ++i)
            {
                densityData->setMassDensity(MassDensitys[i], i);
                densityData->setTemperature(Temperatures[i], i);
            }
            return true;
        }
        //未正常结束
        return false;
    }

    bool FITKAbaqusAdaptorMaterialDensity::adaptW()
    {
        Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialBehaviorType materialType = 
            Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialBehaviorType::Density;
        _materialData = dynamic_cast<Interface::FITKAbaMaterial*>(_dataObj);
        if (!_materialData || !_writer) return false;
        //获得数据结构
        auto tempDensity = _materialData->getMaterialBehavior(materialType);
        //材料为空则返回
        if (!tempDensity) return true;
        auto densityData = dynamic_cast<Interface::FITKAbaMaterialDensity*>(tempDensity);
        if (!densityData) return false;

        QTextStream* stream = _writer->getStream();
        int count = densityData->getNumberOfRows();
        if (count < 1) return false;
        double massDensity, temperature;

        // 写出关键字
        *stream << "*Density" << endl;

        for (int i = 0; i < count; ++i)
        {
            massDensity = densityData->getMassDensity(i);
            *stream << _writer->double2String(massDensity, 9, 18) << ",";

            if (densityData->getUseTemperatureDependentData())
            {
                temperature = densityData->getTemperature(i);

                *stream << _writer->double2String(temperature, 9, 18);
            }
            *stream << endl;

        }
        return true;
    }
}
